package com.tydic.borderer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tydic.borderer.domain.DeclarationFormSummary;
import com.tydic.borderer.domain.DeclarationFormSummaryBordererInfo;
import com.tydic.borderer.domain.bo.DeclarationFormSummaryBo;
import com.tydic.borderer.domain.bo.DeclarationFormSummaryQueryBo;
import com.tydic.borderer.mapper.DeclarationFormSummaryBordererInfoMapper;
import com.tydic.borderer.mapper.DeclarationFormSummaryMapper;
import com.tydic.borderer.service.IDeclarationFormSummaryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class DeclarationFormSummaryServiceImpl implements IDeclarationFormSummaryService {
    private final DeclarationFormSummaryMapper declarationFormSummaryMapper;
    private final DeclarationFormSummaryBordererInfoMapper declarationFormSummaryBordererInfoMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(DeclarationFormSummaryBo declarationFormSummaryRequest) {
        DeclarationFormSummary declarationFormSummary = BeanUtil.toBean(declarationFormSummaryRequest, DeclarationFormSummary.class);
        declarationFormSummaryMapper.insertOrUpdate(declarationFormSummary);
        declarationFormSummaryBordererInfoMapper.delete(Wrappers.lambdaQuery(DeclarationFormSummaryBordererInfo.class).eq(DeclarationFormSummaryBordererInfo::getDeclarationFormSummaryId, declarationFormSummary.getId()));
        List<DeclarationFormSummaryBordererInfo> declarationFormSummaryBordererInfos = declarationFormSummaryRequest.getBordererInfos().stream().map(item -> {
            DeclarationFormSummaryBordererInfo declarationFormSummaryBordererInfo = BeanUtil.toBean(item, DeclarationFormSummaryBordererInfo.class);
            declarationFormSummaryBordererInfo.setDeclarationFormSummaryId(declarationFormSummary.getId());
            return declarationFormSummaryBordererInfo;
        }).collect(Collectors.toList());
        declarationFormSummaryBordererInfoMapper.insertBatch(declarationFormSummaryBordererInfos);

    }

    @Override
    public Page<DeclarationFormSummary> page(DeclarationFormSummaryQueryBo declarationFormSummaryQueryRequest) {
        LambdaQueryWrapper<DeclarationFormSummary> lqw = Wrappers.lambdaQuery(DeclarationFormSummary.class).orderByDesc(DeclarationFormSummary::getCreateDate);
        return declarationFormSummaryMapper.selectPage(declarationFormSummaryQueryRequest.build(), lqw);
    }

    @Override
    public DeclarationFormSummary get(String id) {
        return declarationFormSummaryMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        declarationFormSummaryBordererInfoMapper.delete(Wrappers.lambdaQuery(DeclarationFormSummaryBordererInfo.class).eq(DeclarationFormSummaryBordererInfo::getDeclarationFormSummaryId, id));
        declarationFormSummaryMapper.deleteById(id);
    }

    @Override
    public DeclarationFormSummary getByDeclarationNumber(String declarationNumber) {
        return declarationFormSummaryMapper.selectOne(Wrappers.lambdaQuery(DeclarationFormSummary.class).eq(DeclarationFormSummary::getDeclarationNumber, declarationNumber));
    }

    @Override
    public List<DeclarationFormSummaryBordererInfo> getBordererInfos(String id) {
        return declarationFormSummaryBordererInfoMapper.selectList(Wrappers.lambdaQuery(DeclarationFormSummaryBordererInfo.class).eq(DeclarationFormSummaryBordererInfo::getDeclarationFormSummaryId, id));
    }
}
